<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>角色分配</title>
<block th:replace="commonBase"></block>   
</head>
<script type="text/javascript">
 /*<![CDATA[*/
	mini.parse();
	function setData(data) {
		var action = data.action;//获取指令，是新增 还是修改
	    if (action == "role") {
	        //跨页面传递的数据对象，克隆后才可以安全使用
	        data = mini.clone(data);
	        $("#userId").val(data.userId);
	    }
	    setRole(data.userId);
	}
	function setRole(userId){
	 $.ajax({
	        url: basePath+"user/userRoleIndex",
			type: 'post',
	        data: {'userId':userId},
	        cache: false,
	        success: function (successData) {
	        var message = mini.decode(successData);
	        var roleId = "";
	       for ( var int = 0; int < message.length; int++) {
				roleId += message[int].roleId+",";
				}
				roleId = roleId.substring(0, roleId.length-1);
				mini.get("listbox2").setValue(roleId);
	        },
	        error: function (XMLHttpRequest, textStatus, errorThrown) {
	        	 mini.unmask(document.body);
	        	 showMsg("查询用户角色信息错误","danger",800,"center","center",closeWindow);
	        }
	    });
	}
	function onListBoxValueChanged(e){
		 var listbox = e.sender;
         var value = listbox.getValue();
         var userId = $("#userId").val();
		 $.ajax({
	        url: basePath+"user/toRole",
			type: 'post',
	        data: {'userId':userId,'roleId':value},
	        cache: false,
	        success: function (successData) {
	        	var message = mini.decode(successData);
	        	if(message.type == '200'){
	            	showMsg("角色分配成功","success",800,"center","center");
	        	}else{
	        		showMsg("角色分配失败","danger",800,"center","center");
	        	}
	        	
	        },
	        error: function (XMLHttpRequest, textStatus, errorThrown) {
	        	 mini.unmask(document.body);
	        	 showMsg("用户角色分配错误","danger",800,"center","center",closeWindow);
	        }
	    });
	}
/*]]>*/
</script>
<body>
<input id="userId" name="userId" class="mini-hidden" />
<div id="listbox2" class="mini-listbox" style="width:515px;height:400px;" valueField="id" 
     onvaluechanged="onListBoxValueChanged"   th:attr="url=@{/role/rolelist}" showCheckBox="true" multiSelect="true">     
    <div property="columns">
        <div header="角色名称" field="roleName"></div>
    </div>
</div>
</body>
</html>